package com.weishu.finace.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.weishu.common.core.web.domain.AjaxResult;
import com.weishu.core.entity.*;
import com.weishu.core.service.ICwDealPerDayService;
import com.weishu.core.service.ICwPaymentPlanService;
import com.weishu.core.service.ICwReceiptPlanService;
import feign.QueryMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.ParseException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Author wangyaohan
 * @Date 2021/10/25 16:25
 * @ClassName CwController
 */
@RestController
public class CwController {

    @Resource
    ICwDealPerDayService iCwDealPerDayService;

    @Autowired
    ICwReceiptPlanService iCwReceiptPlanService;

    @Autowired
    ICwPaymentPlanService iCwPaymentPlanService;

    //营业报表
    @GetMapping("/searchCW")
    public AjaxResult searchCW(Integer pageNo, Integer pageSize, String startDate,String endDate){
        QueryWrapper<CwDealPerDay> wrapper = new QueryWrapper<>();
        if (startDate!=null && !"".equals(startDate) && endDate!=null &&!"".equals(endDate)){
           wrapper.between("deal_date",startDate,endDate);
        }
        PageHelper.startPage(pageNo,pageSize);
        List<CwDealPerDay> list = iCwDealPerDayService.list(wrapper);
        PageInfo<CwDealPerDay> info = new PageInfo<>(list);
        return AjaxResult.success(info);
    }

    @GetMapping("/getFee")
    public AjaxResult getFee(String startDate,String endDate) throws ParseException {
        CwDealPerDay cwDealPerDay=iCwDealPerDayService.getFee(startDate,endDate);
        return AjaxResult.success(cwDealPerDay);
    }

    @GetMapping("/getOutFee")
    public AjaxResult getOutFee(String startDate,String endDate) throws ParseException {
        CwDealPerDay cwDealPerDay=iCwDealPerDayService.getOutFee(startDate,endDate);
        return AjaxResult.success(cwDealPerDay);
    }

    //房租收款计划
    @PostMapping("getAllFzFee")
    public AjaxResult getAllFzFee( @RequestBody CwReceiptPlan cwReceiptPlan){
        PageHelper.startPage(cwReceiptPlan.getPageNo(),cwReceiptPlan.getPageSize());
        List<CwReceiptPlan> list = iCwReceiptPlanService.getAllFzFee(cwReceiptPlan);
        PageInfo<CwReceiptPlan> info = new PageInfo<>(list);
        return AjaxResult.success(info);
    }

    @GetMapping("getFzFee")
    public AjaxResult getFzFee() throws ParseException {
        CwDealPerDay cwDealPerDay=iCwReceiptPlanService.getFzFee();
        return AjaxResult.success(cwDealPerDay);
    }

    @PostMapping("getFuKuan")
    public AjaxResult getFuKuan( @RequestBody CwPaymentPlan cwReceiptPlan){
        PageHelper.startPage(cwReceiptPlan.getPageNo(),cwReceiptPlan.getPageSize());
        List<CwPaymentPlan> list = iCwReceiptPlanService.getFuKuanList(cwReceiptPlan);
        PageInfo<CwPaymentPlan> info = new PageInfo<>(list);
        return AjaxResult.success(info);
    }

    @PostMapping("searchSfAll")
    public AjaxResult searchSfAll( @RequestBody CwPaymentPlan cwReceiptPlan){
        PageHelper.startPage(cwReceiptPlan.getPageNo(),cwReceiptPlan.getPageSize());
        List<CwPaymentPlan> list = iCwReceiptPlanService.searchSfAll(cwReceiptPlan);
        PageInfo<CwPaymentPlan> info = new PageInfo<>(list);
        return AjaxResult.success(info);
    }

    @PostMapping("saveFuKuanPlan")
    public AjaxResult saveFuKuanPlan(@RequestBody CwPaymentPlan cwReceiptPlan){
        LocalDate now = LocalDate.now();
        cwReceiptPlan.setPayOrderNo("FKJH"+now.getYear()+"" + now.getMonth().getValue()+ now.getDayOfMonth() + UUID.randomUUID());
        System.err.println("付款单号"+cwReceiptPlan.getPayOrderNo());
        List<CwPaymentPlan> typeList = cwReceiptPlan.getTypeList();
        typeList.forEach(t->{
            //实付金额
            cwReceiptPlan.setRealPaymentAmount(t.getRealPaymentAmount());
            //款项（费用类型）
            cwReceiptPlan.setItemType(t.getItemType());
            //支付金额
            cwReceiptPlan.setPaymentAmount(t.getPaymentAmount());
        });
        //商户ID
        cwReceiptPlan.setBusinessId(1L);
        //创建用户ID
        cwReceiptPlan.setCreateUserid(2L);
        //更新用户ID
        cwReceiptPlan.setUpdateUserid(3L);

        cwReceiptPlan.setCreateTime(new Date());
        cwReceiptPlan.setUpdateTime(new Date());
        System.err.println("添加的数据"+cwReceiptPlan);
        int i=iCwReceiptPlanService.saveFuKuanPlan(cwReceiptPlan);
        if (i>0){
            return  AjaxResult.success(true);
        }
        return AjaxResult.success(false);
    }

    @PostMapping("toUpdate")
    public AjaxResult toUpdate(@RequestBody CwPaymentPlan tCwPaymentPlan){
        CwPaymentPlan byid= iCwPaymentPlanService.getBYId(tCwPaymentPlan.getId());
        return AjaxResult.success(byid);
    }

}
